﻿using PlayListSet.Comm;
using PlayListSet.Entity;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace PlayListSet.UI
{
    /// <summary>
    /// PlayInfoCmdSet.xaml 的交互逻辑
    /// </summary>
    public partial class PlayInfoCmdSetWin : Window
    {
        public ObservableCollection<UserPlayInfoCmd> UserPlayInfoCmds { set; get; } 
        public PlayInfoCmdSetWin()
        {
            InitializeComponent();

            var sources = UserPlayInfoCmd.CopyTo(AppControl.Instance.UserPlayInfoCmds);
            ObservableCollection<UserPlayInfoCmd> userPlayInfoCmds = new ObservableCollection<UserPlayInfoCmd>();
            foreach (var source in sources) 
            {
                userPlayInfoCmds.Add(source);
                source.Cmd.ExecuteEvent += (e) =>
                {
                    var str = (string)e;
                    if (str == "send")
                    {
                        try
                        {
                            var s = UserPlayInfoCmds.FirstOrDefault(i => i.ID == source.ID);
                            if (s != null)
                            {
                                AppControl.Instance.InputControl.HandleCMD(s);
                                MessageBox.Show("执行成功");
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("执行失败：" + ex.Message);
                        }
                    }
                    else if (str == "del")
                    {
                        var s = UserPlayInfoCmds.FirstOrDefault(i => i.ID == source.ID);
                        if (s != null)
                            UserPlayInfoCmds.Remove(s);
                    }
                };
            }
            UserPlayInfoCmds = userPlayInfoCmds;
            this.DataContext = this;
            this.Closed += PlayInfoCmdSetWin_Closed;
        }

     

        private void PlayInfoCmdSetWin_Closed(object sender, EventArgs e)
        {
            dataGrid.IsReadOnly = true;
            dataGrid.CancelEdit();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var source = new UserPlayInfoCmd();
            source.Cmd.ExecuteEvent += (arg) =>
            {
                var str = (string)arg;
                if (str == "send")
                {
                    try
                    {
                        var s = UserPlayInfoCmds.FirstOrDefault(i => i.ID == source.ID);
                        if (s != null)
                        {
                            AppControl.Instance.InputControl.HandleCMD(s);
                            MessageBox.Show("执行成功");
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("执行失败：" + ex.Message);
                    }
                }
                else if (str == "del")
                {
                    var s = UserPlayInfoCmds.FirstOrDefault(i => i.ID == source.ID);
                    if(s!=null)
                        UserPlayInfoCmds.Remove(s);
                }
            };
            UserPlayInfoCmds.Add(source);
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            try
            {
                dataGrid.IsReadOnly = true;
                dataGrid.CancelEdit();
                var sources = UserPlayInfoCmd.CopyTo(UserPlayInfoCmds);
                var t = new List<UserPlayInfoCmd>();
                foreach (var source in sources)
                {
                    t.Add(source);
                }
                t.Sort((i1, i2) => { return i1.Row - i2.Row; });
                AppControl.Instance.UserPlayInfoCmds = t;
                UserPlayInfoCmd.Save(t);
                MessageBox.Show("保存成功");
                dataGrid.IsReadOnly = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show("保存失败："+ex.Message);
            }
          
        }

  
        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            if(MessageBox.Show("确定清空吗？","提示", MessageBoxButton.YesNo, MessageBoxImage.Warning)== MessageBoxResult.Yes)
            {
                UserPlayInfoCmds.Clear();
            }
        }
    }
}
